<template>
	<view class="dp-body" :style="{
		backgroundColor: params.bgcolor, margin: params.margin_y * 2.2 + 'rpx ' + params.margin_x * 2.2 + 'rpx',
		padding: params.padding_y * 2.2 + 'rpx ' + params.padding_x * 2.2 + 'rpx'
	}">
		<view class="dp-content">
			<view class="left" @tap="locationSelect">
				<image class="sl-icon" :src="static_url + '/static/img/juli.png'" />
				<view class="sl-t">{{ regiondata }}</view>
			</view>
		</view>
	</view>
</template>
<script>
var app = getApp();
export default {
	data() {
		return {
			static_url: app.globalData.static_url,
			regiondata: '地址获取中'
		}
	},
	props: {
		params: {},
		data: {}
	},
	mounted() {
		console.log("mounted定位")
		this.getCurrentLocation()
	},
	methods: {
		// 获取当前定位地址
		getCurrentLocation() {
			uni.getLocation({
				type: 'wgs84', // 默认是 wgs84 类型，适合用于 GPS 定位
				success: (res) => {
					const latitude = res.latitude; // 纬度
					const longitude = res.longitude; // 经度
					this.getAreaByLocation(latitude, longitude)
					// this.getCityByLocation(latitude, longitude); // 使用经纬度获取城市名
					console.log(latitude, longitude, '当前经纬度');
				},
				fail: (err) => {
					console.log('获取定位失败:', err);
					this.regiondata = '重新定位'; // 定位失败时显示的提示
				}
			});
		},

		getAreaByLocation(latitude, longitude) {
			app.get('address/getAreaByLocation', {
				latitude: latitude,
				longitude: longitude,
				type: 0,
			}, (res) => {
				if (res.status == 1) {
					const result = res;
					const city = `${result.province}/${result.city}/${result.district}`
					uni.setStorageSync('latitude', latitude)
					uni.setStorageSync('longitude', longitude)
					uni.setStorageSync('city', city)
					uni.setStorageSync('cityName', result.city)
					uni.setStorageSync('areaName', result.district)
					this.regiondata = result.street;

				} else {
					app.error(res.msg)
					this.regiondata = '重新定位'
				}
			});
		},

		// 选择地址
		locationSelect: function () {
			var that = this;
			uni.chooseLocation({
				success: function (res) {
					console.log('地址: ', res);
					// address:"广东省广州市天河区珠江新城花城大道68号"
					// distance:undefined
					// errMsg:"chooseLocation:ok"
					// latitude:23.118104
					// longitude:113.32725
					// name:"环球都会广场"
					// that.info.address = res.name;
					// that.info.latitude = res.latitude;
					// that.info.longitude = res.longitude;
					// that.address = res.name;
					// that.latitude = res.latitude;
					// that.longitude = res.longitude;

					// 使用正则表达式匹配省、市、区和详细地址
					const match = res.address.match(/^(.*?(省|自治区|特别行政区))?(.*?市)?(.*?(区|县|旗))?(.*)$/);

					if (res.name) {
						that.regiondata = res.name;
					}
					// if (match) {
					// 	let province = match[1] || ''; // 省或自治区
					// 	let city = match[3] || ''; // 市
					// 	let district = match[4] || ''; // 区、县或旗
					// 	let address = match[6].trim(); // 详细地址

					// 	console.log("province", province);
					// 	console.log("city", city);
					// 	console.log("district", district);
					// 	console.log("address", address);
					// 	if (province == '' && city != '') {
					// 		province = city;
					// 	}
					// 	that.address = address + res.name;
					// 	that.longitude = res.longitude;
					// 	that.latitude = res.latitude;
					// 	if (province) {
					// 		that.regiondata = province + ',' + city + ',' + district;
					// 	} else {
					// 		that.regiondata = '';
					// 	}
					// } else {
					// 	console.log("地址格式不正确");
					// }
				},
				fail(e) {
					console.log('选择位置失败：', e);
				}
			});
		},
	}
}
</script>
<style lang="scss">
.dp-body {
	height: auto;
	position: relative;
}

.dp-content {
	height: 50rpx;
	width: 100%;

	.left {
		display: flex;
		align-items: center;

		.sl-icon {
			width: 35rpx;
			height: 35rpx;
		}

		.sl-t {
			font-size: 30rpx;
		}
	}
}
</style>